/**
 * 图片控件
 * @author 孙汉斌
 * 参数说明：
 * 1.name：控件名称,为必填项
 * 2.id：可选项,缺省值为name值
 * 3.value:图片路径,可选项。若不为空,则默认加 MyPath.PATH_PHONEPIC路径前缀
 * 4.action：控件动作,可为"edit"和"view"状态,"edit"为上传图片,"view"为读取图片，即非编辑状态。可选项,缺省值为"edit"
 */
package com.zjht.gmcc.bean.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyCode;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyPath;

public class ImgTag extends TagSupport {
	private static final long serialVersionUID = 77L;
	private String name;
	private String id;
	private String value;
	private String action;

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String name = this.name;
		String id = this.id;
		String value = this.value;
		String action = this.action;

		if (MyFormat.isStrNull(name)) {
			throw new JspException("按钮名称不能为空！");
		}
		if (MyFormat.isStrNull(id)) {
			id = name;
		}

		if (MyFormat.isStrNull(value)) {
			value = "";
		}
		if (MyFormat.isStrNull(action)) {// action缺省值为edit
			action = "edit";
		}

		value = value.trim();
		StringBuilder html = new StringBuilder();
		try {
			if ("view".equals(action)) {// 非编辑状态
				html.append("<img id=\"pimg_\" width=\"100\" height=\"100\" src=\"");
				if (value.length() == 0) {
					html.append("/img/null.gif");
				} else {
					html.append(value);
				}
				html.append("\"/>");
			} else if ("edit".equals(action)) {// 编辑状态
			// if (value.length() > 0) {
			// html.append("<input type=\"hidden\" name=\" ");
			// html.append(name);
			// html.append("\" id=\" ");
			// html.append(id);
			// html.append("\" value=\"");
			// html.append(value);
			// html.append("\"/>");
			// }
				String src = value.length() == 0 ? "/img/null.gif" : "/util/img.jsp?now=" + MyCode.getRamCode(6) + "&name=" + value;
				html.append("<img id=\"");
				html.append(id);
				html.append("\" name=\"");
				html.append(name);
				html.append("\" width=\"100\" height=\"100\" src=\"");
				html.append(src);
				html.append("\"/>");
				html.append("<input name=\"");
				html.append(name);
				html.append("\" id=\"");
				html.append(id);
				html.append("\" type=\"file\" size=\"40\" onpropertychange=\"if(this.value!=''){document.getElementById('");
				html.append(id);
				html.append("').src=this.value;}else{document.getElementById('");
				html.append(id);
				html.append("').src='");
				html.append(src);
				html.append("';}\"/>");
				html.append("<br><font color=\"#FF0000\">图片大小：100*100 少于2M、图片格式：JPG或GIF</font>");

			}
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = (value == null ? value : MyPath.PATH_PROVIDE + value);
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

}
